#include "universalinterface.h"
#include <unistd.h>

static UniversalInterface *s_globalInstance = nullptr;

UniversalInterface* UniversalInterface::self()
{
    if (!s_globalInstance) {
        s_globalInstance = new UniversalInterface();
    }
    return s_globalInstance;
}

UniversalInterface::UniversalInterface()
{

}

UniversalInterface::~UniversalInterface()
{
    if (s_globalInstance) {
        delete s_globalInstance;
        s_globalInstance = nullptr;
    }
    if (mUkccPanelGsettings) {
        delete mUkccPanelGsettings;
        mUkccPanelGsettings = nullptr;
    }
    if (mUkuiStyleGsettings) {
        delete mUkuiStyleGsettings;
        mUkuiStyleGsettings = nullptr;
    }
    if (mAccountUserDbus) {
        delete mAccountUserDbus;
        mAccountUserDbus = nullptr;
    }
    if (mFcitxInputMethodDbus) {
        delete mFcitxInputMethodDbus;
        mFcitxInputMethodDbus = nullptr;
    }
}

QGSettings *UniversalInterface::ukccGsettings()
{
    if (!mUkccGsettings) {
        const QByteArray id(UKCC_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mUkccGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mUkccGsettings;
}

QGSettings* UniversalInterface::ukccPanelGsettings()
{
    if (!mUkccPanelGsettings) {
        const QByteArray id(UKCC_PANEL_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mUkccPanelGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mUkccPanelGsettings;
}

QGSettings* UniversalInterface::ukuiStyleGsettings()
{
    if (!mUkuiStyleGsettings) {
        const QByteArray id(UKUI_STYLE_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mUkuiStyleGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mUkuiStyleGsettings;
}

QDBusInterface* UniversalInterface::accountUserDbus()
{
    if (!mAccountUserDbus) {
        unsigned int uid = getuid();
        QString objpath = QString("/org/freedesktop/Accounts/User") + QString::number(uid);
        mAccountUserDbus = new QDBusInterface("org.freedesktop.Accounts",
                                              objpath,
                                              "org.freedesktop.Accounts.User",
                                              QDBusConnection::systemBus());
    }
    return mAccountUserDbus;
}

QDBusInterface* UniversalInterface::fcitxInputMethodDbus()
{
    if (!mFcitxInputMethodDbus) {
        mFcitxInputMethodDbus =  new QDBusInterface("org.fcitx.Fcitx",
                                                    "/inputmethod",
                                                    "org.fcitx.Fcitx.InputMethod",
                                                    QDBusConnection::sessionBus());
    }
    return mFcitxInputMethodDbus;
}


QGSettings* UniversalInterface::ukuiKeyBoardGsettings()
{
    if (!mKeyboardGsettings) {
        const QByteArray id(UKUI_KEYBOARD_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mKeyboardGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mKeyboardGsettings;
}

QGSettings* UniversalInterface::ukccOsdGsettings()
{
    if (!mUkccOsdGsettings) {
        const QByteArray id(UKCC_OSD_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mUkccOsdGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mUkccOsdGsettings;
}

QGSettings* UniversalInterface::ukuiMouseGsettings()
{
    if (!mUkuiMouseGsettings) {
        const QByteArray id(UKUI_MOUSE_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mUkuiMouseGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mUkuiMouseGsettings;
}

QGSettings* UniversalInterface::mateInterfaceGsettings()
{
    if (!mMateInterfaceGsettings) {
        const QByteArray id(MATE_INTERFACE_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mMateInterfaceGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mMateInterfaceGsettings;
}

QGSettings *UniversalInterface::ukuiNoticeGsettings()
{
    if (!mUkuiNoticeGsettings) {
        const QByteArray id(UKUI_NOTICE_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mUkuiNoticeGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mUkuiNoticeGsettings;
}

QGSettings *UniversalInterface::gnomeVinoGsettings()
{
    if (!mGnomeVinoGsettings) {
        const QByteArray id(GNOME_VINO_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mGnomeVinoGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mGnomeVinoGsettings;
}

QGSettings *UniversalInterface::ukccNoticeGsettings()
{
    if (!mUkccNoticeGsettings) {
        const QByteArray id(UKCC_NOTICE_ID);
        if (QGSettings::isSchemaInstalled(id)) {
            mUkccNoticeGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mUkccNoticeGsettings;
}

QDBusInterface * UniversalInterface::ukccSystemDbus()
{
    if (!mUkccSystemDbus) {
        mUkccSystemDbus = new QDBusInterface("com.control.center.qt.systemdbus",
                                             "/",
                                             "com.control.center.interface",
                                             QDBusConnection::systemBus());
    }
    return mUkccSystemDbus;
}

QGSettings *UniversalInterface::mateBackgroundGsettings()
{
    if (!mBackgroundGsettings) {
        const QByteArray id("org.mate.background");
        if (QGSettings::isSchemaInstalled(id)) {
            mBackgroundGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mBackgroundGsettings;
}

QGSettings *UniversalInterface::screensaverGsettings()
{
    if (!mScreensaverGsettings) {
        const QByteArray id("org.ukui.screensaver");
        if (QGSettings::isSchemaInstalled(id)) {
            mScreensaverGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mScreensaverGsettings;
}

QGSettings *UniversalInterface::screensaverDefaultGsettings()
{
    if (!mScreensaverDefaultGsettings) {
        const QByteArray id("org.ukui.screensaver-default");
        if (QGSettings::isSchemaInstalled(id)) {
            mScreensaverDefaultGsettings = new QGSettings(id, QByteArray());
        }
    }
    return mScreensaverDefaultGsettings;
}
